<?php

namespace App\Http\Controllers\Develop;

use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Models\User\Task;
use App\Models\User\TaskLog;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis as Redis;

class TaskController extends Controller
{
    public function index(Request $request)
    {
        $task_type = $request->input('task_type', 1);
        $data['task_type'] = $task_type;

        return view ('develop/TaskList',$data);
    }

    //日志列表
    public function search(Request $request)
    {
        $where = [];

        $task_name = $request->input('task_name');
        $task_link = $request->input('task_link');
        $task_type = $request->input('task_type');

        if (!empty($task_name)) {
            $where[] = ['task_name',$task_name];
        }
        if (!empty($task_link)) {
            $where[] = ['task_link',$task_link];
        }
        if (!empty($task_type)) {
            $where[] = ['task_type',$task_type];
        }

        $task_data = Task::where($where)
            ->orderBy($request->input('sort'), $request->input('order'))
            ->paginate($request->input('limit'))
            ->toArray();

        //返回数组
        $return = [
            'total' => $task_data['total'],
            'rows' => []
        ];

        if ( $task_data['data'] ) {

            foreach( $task_data['data'] as $row ){

                if ($row['task_status'] == 1){
                    $task_status = '运行中';
                }else{
                    $task_status = '暂停';
                }

                $operation = '';
                $operation .='<a href="javascript: void(0);" onclick="sysTask.log(' . $row['task_id'] . ')">日志</a>&nbsp;&nbsp;
                            <a href="javascript: void(0);" onclick="sysTask.edit(' . $row['task_id'] . ')">修改</a>&nbsp;&nbsp;
                            <a href="javascript: void(0);" onclick="sysTask.del(' . $row['task_id'] . ')">删除</a>&nbsp;&nbsp;';
                if( $row['task_status'] == 1 ){
                    $operation.= '<a href="javaScript:void(0);" onclick="sysTask.changeStatus('.$row['task_status'].',' . $row['task_id'] . ');" >暂停</a>&nbsp;&nbsp;&nbsp;';
                }else{
                    $operation.= '<a href="javaScript:void(0);" onclick="sysTask.changeStatus('.$row['task_status'].',' . $row['task_id'] . ');" >运行</a>&nbsp;&nbsp;&nbsp;';
                }

                $return['rows'][] = [
                    'operation' => $operation,
                    'task_name' => $row['task_name'],
                    'task_link' => $row['task_link'],
                    'task_act_value' => $row['task_act_value'],
                    'task_status' => $task_status
                ];

            }

        }

        return response()->json($return);

    }

    //获取任务信息
    public function get($task_id)
    {

        $task_one = Task::find($task_id);

        return response()->json(['code'=>200, 'message'=>'ok', 'data'=>$task_one]);

    }

    //获取日志信息
    public function log($task_id)
    {

        $task_one = TaskLog::where(['task_id'=>$task_id])->get()->toArray();

        if (empty($task_one)){
            $task_one = '';
        }
        return response()->json(['code'=>200, 'message'=>'ok', 'data'=>$task_one]);

    }

    //保存任务信息
    public function store(Request $request)
    {
        $task_act_value = $request->input('task_act_value');
        $task_link = $request->input('task_link');
        $task_name = $request->input('task_name');
        $task_type = $request->input('task_type');
        $task_id = $request->input('task_id');

        if ( empty($task_act_value) ) {
            return response()->json(array(
                'code' => 100001,
                'message' => 'act值不能为空'
            ));
        }

        if ( !isset($task_link) ) {
            return response()->json(array(
                'code' => 100002,
                'message' => '任务链接不能为空'
            ));
        }

        if ( empty($task_name) ) {
            return response()->json(array(
                'code' => 100003,
                'message' => '任务名称不能为空'
            ));
        }
        $task = Task::find($task_id);

        if ( !$task ) {
            $task = new Task();
            $task->task_status = 1;
            $task->uuid = makeUuid();
            $task->timeStamp = Carbon::now();
            $task->creator = Redis::get('WDH_ADMIN_USER_ID_' . session_id());
            $task->createTime = Carbon::now();
        }
        $task->task_type = $task_type;
        $task->task_act_value = $task_act_value;
        $task->task_name = $task_name;
        $task->task_link = $task_link;

        $task->save();

        return response()->json(['code' => 200, 'message' => '保存成功']);
    }

    //删除任务信息
    public function delete($task_id)
    {

        $task = Task::find($task_id);

        if ( $task ) {
            $task->delete();
        }

        return response()->json(['code' => 200, 'message' => '删除成功']);

    }

    //修改状态
    public function status(Request $request)
    {
        $task_id = $request->input('task_id');
        $task_status = $request->input('task_status');
        if ( empty( $task_id ) ) {
            return response()->json(['code' => 10000, 'message' => '参数错误']);
        }

        $task_data = Task::find($task_id);
        if ( $task_data->task_status == 1 ) {
            $task_data->task_status = 2;
        } else if ( $task_data->task_status == 2 ) {
            $task_data->task_status =1;
        }
        $task_data->save();

        if ( $task_data->task_status == 2 ) {
            return response()->json([
                'message'=>'任务暂停成功',
                'code'=>200
            ]);
        } else if ($task_data->task_status == 1) {
            return response()->json([
                'message'=>'任务运行成功',
                'code'=>200
            ]);
        }

    }
}
